<!DOCTYPE html>
<html lang="en">
<head>
    <title>学生</title>
    <script src="js/jquery-3.6.3.min.js"></script>
    <script>
        $(function () {
            //保存
            $("form#addForm").submit(function () {
                //>>>
                //$.post("addStu.do",{sname:$("#sname").val(),sex:$},)
                //没有上传，一步序列化整个form
                // $.post("addStu.do",$("form#addForm").serialize(),function () {
                //
                // });
                //
                var f=$("#addForm")[0];//转换成dom对象
                //alert(f.action+">>>>>");
                var mydata=new FormData(f);//mydata.appendyekey也可以
                $.ajax({
                    url:"addStu.do",
                    type:"POST",
                    data:mydata,
                    contentType:false,
                    processData:false,
                    success:function(d){
                        if(d.code==0){
                            alert("成功");
                            location.replace('students.html');
                        }else{
                            alert("失败:"+d.msg);
                        }
                    }
                });

                //
                return false;
            });
            $.get("grades2.do",function (grades) {
                let g=$("#gradeid");
                $.each(grades,function (i,o) {
                    g.append(new Option(o.gname,o.id));
                    //g.append("<option value='"+o.id+"'>"+o.gname+"</option>")
                })
            });
            $("#telephone").blur(function () {
                if (this.value == "")
                    return;

                let that = this;
                //json的格式改造
                $.post("checkPhone2.do", {phone: $(this).val()}, function (data) {
                    data=JSON.parse(data);
                    if (data.ok) {
                        $(that).next().text("此手机号已经被注册");
                    } else {
                        $(that).next().text("此手机号可以使用");
                    }
                });


            });
        });
    </script>

</head>
<body>
<form id="addForm" action="doAddStudent2.jsp" method="post" enctype="multipart/form-data">
    <label>名字</label>
    <input type="text" name="sname" value=""><br>
    <label> 性别</label>
    <select name="sex"><br>
        <option name="男">男</option>
        <option name="女">女</option>
        <option name="妖">妖</option>
    </select>
    <label>电话</label>
    <input type="tel" name="telephone" value="" id="telephone"><span style="color:red"></span><br>
    <label>QQ</label>
    <input type="text" name="qq" value=""><br>
    <label>微信</label>
    <input type="text" name="wechat" value=""><br>
    <label>年级号</label>

    <label>年级号：</label>
    <select id="gradeid" name="gradeid" id="gradeid">
    </select>

    <br>
    <label>入学时间</label>
    <input type="date" name="enterdate" value=""><br>
    <label>出生城市</label>
    <input type="text" name="fromcity" value=""><br>
    <label>生日</label>
    <input type="date" name="birthday" value=""><br>
    <label>出生地址</label>
    <input type="text" name="address" value=""><br>
    <label>密码</label>
    <input type="text" name="pass" value=""><br>
    <label>身份证照片</label>
    <input type="file" name="idcardpic"><br>
    <input type="submit" name="提交">
</form>
</body>
</html>